

const tool = require('../tool');

const fs = require('fs');
const { generateFunc } = require('../tool');

//添加页面的新增
module.exports = async function addPageGenerate(module_config, detailPageConfig) {

  //是否初次生成的字段
  var isGenerate = detailPageConfig.isGenerate;
  if (isGenerate == 'false') {
    return;
  }

  var detailGroupTpl = ``;
  var detailMethodCode = ``;

  detailMethodCode = getDetailMethodsCode(detailPageConfig);
  // console.log(detailMethodCode);

  //展示
  var detailGroupItems = '';
  detailGroupItems = getDetailItem(detailPageConfig);
  detailGroupTpl = getDetailConTpl(detailGroupItems);

  //数据
  var dataStr = "";
  dataStr = getDetailDataStr(detailPageConfig);

  var curPageName = "detail";
  if (detailPageConfig.detail_page_name) {
    curPageName = detailPageConfig.detail_page_name;
  }

  //引入去除未使用的插值字符串
  const finalHandle = require('../module/final_handle'); //最后处理未使用的数据

  //生成文件
  tool.generateFunc({
    tipsText: detailPageConfig.tipsText,
    filePath: `./template/detail_page_tpl.vue`,
    outputPath: `../src/pages/${module_config.module_name}/${curPageName}.vue`,
    templateCode: detailGroupTpl,
    // tableDefaultMounted,
    // tableDefaultMethods,
    methodsCodeFirst: detailMethodCode,
    // methodsCodeSecond: tableMethodsSecond,
    dataCode: dataStr,
    // styleCode: '',
  }, finalHandle);
}

//详情展示组件
function getDetailConTpl(detailStr) {
  return `
    <div class="edit-wrap">
      <div class="top-input">
        ${detailStr}
      </div>
    </div>
  `;
}

//数据
function getDetailDataStr(detailPageConfig) {
  var formDataItemStr = ``;
  var optShowStr = ``;
  for (var itemKey in detailPageConfig.formData) {
    var detailItem = detailPageConfig.formData[itemKey];
    formDataItemStr += `
      ${detailItem.inputName}: "",`;
    if (detailItem.type == 'opts') {
      optShowStr += detailItem.optShowName + ":" + detailItem.optShowStr + ",";
    }
  }
  formDataItemStr = `
    formData: {
      ${formDataItemStr}
    },
  `;

  var detailStr = formDataItemStr + optShowStr;
  return detailStr;
}

//详情项
function getDetailItem(detailPageConfig) {
  var detailItemStr = "";
  var formData = detailPageConfig.formData;
  for (var itemKey in formData) {
    if (itemKey.includes('item') && !itemKey.includes('item_img')) {
      var dataItem = formData[itemKey];
      var { labelName, inputName, frontTips, behindTips } = dataItem;
      if (!frontTips) frontTips = "";
      if (!behindTips) behindTips = "";

      if (dataItem.type == "normal" || !dataItem.type) {
        detailItemStr += `
          <div class="item">
            <label for>${labelName}:</label>
            <div class="inline-block">${frontTips} {{formData.${inputName}}} ${behindTips}</div>
          </div> 
        `;

      } else if (dataItem.type == "opts") {
        var { optShowName } = dataItem;
        detailItemStr += `
          <div class="item">
            <label for>${labelName}:</label>
            <div class="inline-block">${frontTips} {{${optShowName}[formData.${inputName}]}} ${behindTips}</div>
          </div> 
        `;

      } else if (dataItem.type == 'innerCode') {
        var { innerCode } = dataItem;
        detailItemStr += `
        <div class="item">
          <label for>${labelName}:</label>
          <div class="inline-block">
            ${frontTips} ${innerCode} ${behindTips}
          </div>
        </div> 
      `;

      } else if (dataItem.type == "img") {
        detailItemStr += `
        <div class="item img-item">
          <label for>${labelName}:</label>
          <div class="inline-block">
            <div class="img-box">
              <img v-if="formData.${inputName}" :src="formData.${inputName}" alt="图片" />
            </div>
          </div>
        </div> 
      `;
      }

    }
  }

  return detailItemStr;
}


// 详情方法
function getDetailMethodsCode(detailPageConfig) {
  var url = detailPageConfig.detail_api,
    params = detailPageConfig.request_params ? detailPageConfig.request_params : `{id}`;

  return `
  //获取编辑数据
  getDetailData (id) {
    let url = ${url};
    let data = ${params};
    console.log("参数", data);
    jhTools.get(
      url,
      null, 
      data,
      (res) => {
        console.log("结果", res);
        if (res.code == 200) {
         this.formData = res.data;
        }
      },
      (err) => {
        console.log(err);
      }
    );
  },
  `;
}


